package org.deschutter.scans;

import org.deschutter.constants.Config;
import org.deschutter.exceptions.AddJGPLineException;
import org.deschutter.exceptions.DeriveIntelException;
import org.deschutter.scans.jumpgate.Fleet;
import org.deschutter.scans.jumpgate.Friendly;
import org.deschutter.scans.jumpgate.Hostile;
import org.deschutter.scans.jumpgate.Return;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Berten De Schutter
 * Date: 01-Jul-2009
 * Time: 11:27:21
 */
public class Jumpgate extends Scan {

    private ArrayList<Fleet> fleets;


    public Jumpgate(String ID, BigDecimal tick, String x, String y, String z, String scanpage, BigDecimal userID, Session session) {
        super(ID, Config.JUMPGATE_PROBE, tick, x, y, z, scanpage, userID, session);
    }

    @Override
    protected String parseScan() {

        ArrayList<Fleet> fleets = new ArrayList<Fleet>();
        Pattern compiledRegex;
        Matcher regexMatcher;


        // Get Fleets
        compiledRegex = Pattern.compile("<td[^>]*>(\\d+)\\:(\\d+)\\:(\\d+)</td><td[^>]*>([^<]+)</td><td[^>]*>([^<]+)</td><td[^>]*>(\\d+)</td><td[^>]*>(\\d+)</td>");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));

        while (regexMatcher.find()) {
            if (regexMatcher.group(4).equals("Defend")) {
                fleets.add(new Friendly(regexMatcher.group(1), regexMatcher.group(2), regexMatcher.group(3), new Integer(regexMatcher.group(6)), new Integer(regexMatcher.group(7)), regexMatcher.group(5), this));
            } else if (regexMatcher.group(4).equals("Attack")) {
                fleets.add(new Hostile(regexMatcher.group(1), regexMatcher.group(2), regexMatcher.group(3), new Integer(regexMatcher.group(6)), new Integer(regexMatcher.group(7)), regexMatcher.group(5), this));
            } else {   // Return
                fleets.add(new Return(regexMatcher.group(1), regexMatcher.group(2), regexMatcher.group(3), new Integer(regexMatcher.group(6)), new Integer(regexMatcher.group(7)), regexMatcher.group(5), this));
            }
        }
        Transaction tx = null;
        try {
            tx = getSession().beginTransaction();
            for (Fleet fleet : fleets) {    // Add Intel
               // fleet.deriveIntel(getX(), getY(), getZ(), getTick(), getID());
                getSession().saveOrUpdate(fleet.getJGP());
            }
            tx.commit();
        //} catch (DeriveIntelException e) {
            //return "Could not derive intel for " + getX() + ":" + getY() + ":" + getZ() + " Scan ID: " + getID() + "Tick: " + getTick() + ". Reason:" + e.getError();
        } catch (AddJGPLineException e) {
            if (tx != null) tx.rollback();
            return "Could not ADD JGP Line for " + getX() + ":" + getY() + ":" + getZ() + " Scan ID: " + getID() + "Tick: " + getTick() + ". Reason:" + e.getError();
        }
        return "JGP on " + getX() + ":" + getY() + ":" + getZ() + " parsed successfully";
    }

    public BigDecimal getTypeID() {
        return SCAN_TYPE_JGP;
    }
}
